package cs575.wido.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JTable;

import cs575.wido.action.CompletedTaskAction;
import cs575.wido.action.CurrentTaskAction;
import cs575.wido.action.NewGroupAction;
import cs575.wido.action.NewTaskAction;
import cs575.wido.action.PendingTaskAction;
import cs575.wido.action.ViewGroupAction;
import cs575.wido.action.ViewTaskAction;
import cs575.wido.model.CurrentTaskModel;
import cs575.wido.model.GroupModel;
import cs575.wido.model.TaskModel;
import javax.swing.JScrollPane;

public class GroupListWindow extends JDialog {
	private Map<String, Action> actions;

	private JButton btnBack;
	private JLabel lblGroup;
	private JScrollPane scrollPane;
	private JTable groupTable;
	private GroupModel groupModel;
	private JButton btnGroupDetails;
	private JButton btnNewGroup;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			GroupListWindow dialog = new GroupListWindow();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public GroupListWindow() {
		setTitle("My Group");
		actions = new HashMap<String, Action>();
		buildActions();
		
		getContentPane().add(buildHeaderPane(), BorderLayout.NORTH);
		getContentPane().add(buildButtonPane(), BorderLayout.SOUTH);
		
		groupModel = new GroupModel();
		groupTable = new JTable(groupModel);
		scrollPane = new JScrollPane(groupTable);
		
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		pack();		
	}
	
	private void buildActions() {
		actions.put("new-group", new NewGroupAction());
		actions.put("view-group", new ViewGroupAction(this));
	}
	
	private JComponent buildHeaderPane() {
		JPanel headerPane = new JPanel();
		headerPane.setLayout(new BorderLayout(0, 0));
		
		lblGroup = new JLabel("Group");
		lblGroup.setFont(new Font("Tahoma", Font.PLAIN, 20));
		headerPane.add(lblGroup, BorderLayout.WEST);
		
		return headerPane;
	}
	
	private JComponent buildButtonPane() {
		JPanel buttonPane = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPane.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		
		btnNewGroup = new JButton("New Group");
		btnNewGroup.setAction(actions.get("new-group"));
		buttonPane.add(btnNewGroup);
		
		btnGroupDetails = new JButton("Group Details");
		btnGroupDetails.setAction(actions.get("view-group"));
		buttonPane.add(btnGroupDetails);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GroupListWindow.this.dispose();
			}
		});
		buttonPane.add(btnBack);
		
		return buttonPane;
	}
	
	public AbstractTableModel getGroupModel() {
		return groupModel;
	}

	public void setTaskModel(GroupModel groupModel) {
		this.groupModel = groupModel;
		this.groupTable.setModel(groupModel);
		this.groupModel.fireTableDataChanged();
	}

	public JTable getGroupTable() {
		return groupTable;
	}
	
        /*
	public cs575.client.GroupEx getGroup() {
		int selectedRow = this.groupTable.getSelectedRow();
		return this.groupModel.getGroupAt(selectedRow);
	}*/
        
        public String[] getGroup() {
            int selectedRow = this.groupTable.getSelectedRow();
            return this.groupModel.getGroup(selectedRow);
        }
}
